package cn.iocoder.yudao.module.vlog.statistics.dal.mysql;

import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortablePageParam;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.vlog.statistics.dal.dataobject.VLogStatisticsDO;
import cn.iocoder.yudao.module.vlog.statistics.vo.VLogStatisticsReqVO;
import cn.iocoder.yudao.module.vlog.statistics.vo.VLogStatisticsRespVO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 视频统计 Mapper
 *
 * @author 山野羡民（1032694760@qq.com）
 */
@Mapper
public interface VLogStatisticsMapper extends CoreMapper<VLogStatisticsDO> {

    default PageResult<VLogStatisticsDO> selectPageGroupByUserId(VLogStatisticsReqVO reqVO, SortablePageParam pageParam) {
        return selectPage(pageParam, buildWrapper(reqVO)
                .select(VLogStatisticsDO::getUserId)
                .groupBy(VLogStatisticsDO::getUserId));
    }

    default VLogStatisticsRespVO selectVoByTimeBetween(VLogStatisticsReqVO reqVO) {
        return selectJoinOne(VLogStatisticsRespVO.class, buildWrapper(reqVO));
    }

    default List<VLogStatisticsDO> selectListByTimeBetween(VLogStatisticsReqVO reqVO) {
        return selectList(buildWrapper(reqVO)
                .select(VLogStatisticsDO::getTime)
                .groupBy(VLogStatisticsDO::getTime));
    }


    /**
     * 构建 LambdaWrapper
     *
     * @param reqVO 查询参数
     * @return LambdaWrapper
     */
    private static MPJLambdaWrapperX<VLogStatisticsDO> buildWrapper(VLogStatisticsReqVO reqVO) {
        return new MPJLambdaWrapperX<VLogStatisticsDO>()
                .betweenIfPresent(VLogStatisticsDO::getTime, reqVO.getTimes())
                .selectCount(VLogStatisticsDO::getUserId, "userCount")
                .selectSum(VLogStatisticsDO::getVlogCount)
                .selectSum(VLogStatisticsDO::getFansCount)
                .selectSum(VLogStatisticsDO::getFollowCount)
                .selectSum(VLogStatisticsDO::getReceiveLikeCount)
                .selectSum(VLogStatisticsDO::getSendLikeCount)
                .selectSum(VLogStatisticsDO::getReceiveFavoriteCount)
                .selectSum(VLogStatisticsDO::getSendFavoriteCount)
                .selectSum(VLogStatisticsDO::getReceiveShareCount)
                .selectSum(VLogStatisticsDO::getSendShareCount);
    }

}
